$(function() {
	//panel初始化
	$('#dDepartPanel').panel({
		border:false
	});
	
	$('#addDepartBtn').linkbutton({plain:true,iconCls:'icon-depart-add'});
	$('#editDepartBtn').linkbutton({plain:true,iconCls:'icon-depart-edit'});
	$('#delDepartBtn').linkbutton({plain:true,iconCls:'icon-depart-del'});
	$('#closeDepartBtn').linkbutton({plain:true,iconCls:'icon-close'});
	
	//treegrid初始化
	$('#dDepartTreeGrid').treegrid( {
		nowrap : false,
		collapsible:true,
		loadMsg:'数据加载中......',
		idField : 'id',
		//url:'departAjax!loadCurrUserFullDeparts',
		url:'departAjax!loadDepartData',
		treeField : 'text',
		border:false,
		columns : [[
		    {title:'名称',field:'text',width:300},
		    {title:'代码',field:'code',width:100},
		    {title:'描述',field:'des',width:200},
		    {title:'层次',field:'treeSign',width:500}
		]],
		onDblClickRow: function(row){
			$(this).treegrid('toggle',row.id);
		}
	});
	
	$('#dDepartTreeGrid').treegrid('toggle', 0);
	
	//添加窗口
	$('#dDepartWindow').window({
		title:'增加',
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-depart',
		width:300,
		height:210
	});	
});

//打开窗口
function openDepartWindow(type) {
	$('#departName').val('');
	$('#departCode').val('');
	$('#departDes').val('');
	var node = $('#dDepartTreeGrid').treegrid('getSelected');
	if(node==null){
		$.messager.alert('提示','请先选择一个部门!');
		return;
	}else{
		//删除部门
		if(type == 'del'){
			delDepart();
			return;
		}
		//增加部门
		else if(type == 'add'){
			$('#departMethodName').val('add');
			$('#dDepartWindow').window({title:'增加'});
			$('#submitDepartButton').val('增加');
		}
		//编辑部门
		else if(type == 'edit'){
			$('#departMethodName').val('edit');
			$('#dDepartWindow').window({title:'编辑'});
			$('#submitDepartButton').val('编辑');
			$('#departName').val(node.text);
			$('#departCode').val(node.code);
			$('#departDes').val(node.des);
		}else{
			$.messager.alert('提示','非法操作!',"error");
			return;
		}
	}

	$('#dDepartWindow').window('open');	
	$('#departName').focus();
}

//提交操作(添加主班组,添加子班组,修改班组)
function submitDepart(){
	var departMethodName = $('#departMethodName').val();
	var departName = $('#departName').val();
	var departCode = $('#departCode').val();
	var departDes = $('#departDes').val();
	if(departName == ''){
		$.messager.alert('提示','名称不能为空!');
		return;
	}

	switch(departMethodName){
		//添加班组
		case 'add':
			var node = $('#dDepartTreeGrid').treegrid('getSelected');
		    $.post('departAjax!add', {treeSign:node.treeSign, parentId:node.id, name:departName, code: departCode, des: departDes},
		        	function(data){
				    	$('#dDepartTreeGrid').treegrid('reload');
		        	}
		        	, 'json');
			break;
		//修改班组
		case 'edit':
			var node = $('#dDepartTreeGrid').treegrid('getSelected');
		    $.post('departAjax!modify', {id:node.id, name:departName, code: departCode, des: departDes},
		        	function(data){
		    			$('#dDepartTreeGrid').treegrid('reload');
		        	}
		        	, 'json');
			break;
	}
	
	$('#dDepartWindow').window('close');	
}

//删除班组
function delDepart(){

	var selectedNode = $('#dDepartTreeGrid').treegrid('getSelected');
	var childrenNodes = null;
	if(selectedNode != null){
		childrenNodes = $('#dDepartTreeGrid').treegrid('getChildren', selectedNode.id);
	}

	if(selectedNode==null){
		$.messager.alert('提示','请先选择一个部门!');
		return;
	}else if(selectedNode.id == 1){
		$.messager.alert('提示','不能删除根部门!');
		return;
	}else if(childrenNodes.length != 0){
		$.messager.alert('提示','请先删除所有子部门!');
		return;
	}else{
		$.post('userAjax!existUserByDepart', {departId:selectedNode.id},
	        	function(data){
					if(data.result == 'true'){
						$.messager.alert('提示','该部门下还存在用户!');
						return;
					}else{
						$.post('departAjax!delete', {id:selectedNode.id},
					        	function(data){
									$('#dDepartTreeGrid').treegrid('reload');
					        	}
					        	, 'json');
					}
	        	}
	        	, 'json');
	}
}